// 程序入口类
class GameMain {
    //public net : Net;
    public login : Login;
    public viewStart : view.Start;
    public viewFarm : view.Farm;
    public viewWarehouse : view.dlg.warehouse;
    public serverTime : number;
    public gainAward : view.ani.GainAward;
    public layerDialog : Laya.Sprite;
    public layerPrompt : Laya.Sprite;

    constructor()
    {
		//初始化微信小游戏
		Laya.MiniAdpter.init();
        Laya.init(720,1280, Laya.WebGL);
        Laya.stage.bgColor = "#d3f9f6";

        //设置屏幕缩放模式
        Laya.stage.scaleMode = Laya.Stage.SCALE_FIXED_AUTO;
        //设置屏幕水平居中
        Laya.stage.alignH = Laya.Stage.ALIGN_CENTER;
        //设置屏幕垂直居中
        Laya.stage.alignV = Laya.Stage.ALIGN_MIDDLE;
        //设置自动竖屏
        Laya.stage.screenMode = Laya.Stage.SCREEN_VERTICAL;

        //初始化kbe
        var args = new KBEngine.KBEngineArgs();
        args.ip = ServerCfg.ServerLocalUrl;
        if (Laya.Browser.onWeiXin) {
            args.ip = ServerCfg.ServerUrl;
        }
        args.port = ServerCfg.ServerPort;
        args.serverHeartbeatTick = 2;
        args.reloginSpace = 6;
        KBEngine.create(args);

        this.serverTime = 0;
        Laya.timer.loop(Cfg.LoopTime, this, this.OnLoop);

        this.layerDialog = new Laya.Sprite();
        this.layerPrompt = new Laya.Sprite();

		//激活资源版本控制
        let versionFileUrl = "version.json";
        if (Laya.Browser.onWeiXin) {
            versionFileUrl = ServerCfg.ResUrl + versionFileUrl;
            wx.showShareMenu({withShareTicket : true});
        }
		Laya.ResourceVersion.enable(versionFileUrl, Laya.Handler.create(this, this.BeginLoad), Laya.ResourceVersion.FILENAME_VERSION);
    }

    OnLoop() : void {
        this.serverTime += 1;
    }

    OnResize() : void {
        //地面元素随画布缩放
        let scalex = Laya.stage.width / Laya.stage.designWidth;
        let scaley = Laya.stage.height / Laya.stage.designHeight;
        this.viewFarm.boxGround.scale(scalex, scaley);
    }

	BeginLoad() : void {
        //加载本地登录资源
        Laya.loader.load([{url:"res/atlas/start.atlas", type:Laya.Loader.ATLAS}], Laya.Handler.create(this,this.onProloaded));
	}

    onProloaded():void{
        // 登录界面初始化,进行资源加载,携带回调函数
        this.viewStart = new view.Start();
        //显示登录界面
        Laya.stage.addChild(this.viewStart);
    }

    //资源加载完成
    OnLoaded() : void {
        Cfg.Init();
        //this.net = new Net();
        this.gainAward = new view.ani.GainAward();
        this.viewFarm = new view.Farm();

        //初始化管理器
        CtrlManager.Init();
        //初始化仓库数据
		WarehouseModel.Init();
        MailModel.Init();

        this.OnResize();
        //监听舞台大小改变
        Laya.stage.on(Laya.Event.RESIZE, this, this.OnResize);
        
        UIConfig.popupBgAlpha = 0.8;
        
        //资源加载完成后，开始微信登录
        this.login = new Login();
        this.login.Login();
    }
}
let Game : GameMain = new GameMain();